package study.reactive.springboot.webflux.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.Duration;

@Slf4j
@RestController
public class TestingController {

    private String HOST = "http://152.136.206.109:7500";

    @GetMapping(value = "/sleep/{times}")
    public Mono<String> hello(@PathVariable int times) {
        log.info("sss");
        Mono<String> mono = Mono.delay(Duration.ofMillis(times)).thenReturn("Hello").doOnNext(e -> {
            log.info("222222222", e);
        });
        log.info("aaaa");
        return mono;
    }


    @GetMapping(value = "/testing/reactor/{times}")
    public Mono<String> reactor(@PathVariable int times) {
        log.info("aaaaaaaaaaaaaaaaa");
        return Mono.just(times)
                .flatMap(t -> WebClient.create(HOST)
                        .get()
                        .uri("/sleep/" + times)
                        .retrieve()
                        .bodyToMono(String.class)
                        .doOnNext(e -> {
                            log.info("sssssssssss");
                        }));
    }
}
